package cn.ldj.ztomcat.servlets;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.ldj.ztomcat.catalina.Context;
import cn.ldj.ztomcat.http.MyRequest;
import cn.ldj.ztomcat.http.MyResponse;
import cn.ldj.ztomcat.util.Constant;
import cn.ldj.ztomcat.util.WebXMLUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

public class DefaultServlet extends HttpServlet {
    private static DefaultServlet instance = new DefaultServlet();

    public static synchronized DefaultServlet getInstance() {
        return instance;
    }

    private DefaultServlet() {

    }

    public void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
            throws IOException, ServletException {
        MyRequest request = (MyRequest) httpServletRequest;
        MyResponse response = (MyResponse) httpServletResponse;

//        Context context = request.getContext();

        String uri = request.getUri();
        if ("/500.html".equals(uri))
            throw new RuntimeException("this is a deliberately created exception");

        if ("/".equals(uri))
            uri = WebXMLUtil.getWelcomeFile(request.getContext());

        String fileName = StrUtil.removePrefix(uri, "/");

//        File file = FileUtil.file(context.getDocBase(), fileName);
        File file = FileUtil.file(request.getRealPath(fileName));

        if (file.exists()) {
            System.out.println("请求对应的文件路径=" + file.getAbsolutePath());
            String extName = FileUtil.extName(file);
            String mimeType = WebXMLUtil.getMimeType(extName);
            response.setContentType(mimeType);

            byte body[] = FileUtil.readBytes(file);
            response.setBody(body);

            if (fileName.equals("timeConsume.html"))
                ThreadUtil.sleep(1000);

            response.setStatus(Constant.CODE_200);
        } else {
            response.setStatus(Constant.CODE_404);
        }

    }

}
